﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using Org.BouncyCastle.Crypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class RecipeConfigService : IRecipeConfigService
    {
        private readonly IRecipeConfigOperate _recipeConfigOperate;
        private readonly IRecipeTypeOperate _recipeTypeOperate;
        private readonly IRecipeOperate _recipeOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public RecipeConfigService(IRecipeOperate recipeOperate,IUnitOfWork unitOfWork, IMapper mapper, IRecipeConfigOperate recipeConfigOperate, IRecipeTypeOperate recipeTypeOperate)
        {
            _mapper = mapper; 
            _unitOfWork = unitOfWork;
            _recipeConfigOperate = recipeConfigOperate;
            _recipeTypeOperate = recipeTypeOperate;
            _recipeOperate = recipeOperate;
        }

        public List<RecipeConfigModel> MendingRange(List<RecipeConfig> entities)
        {
            List<RecipeConfigModel> models = new List<RecipeConfigModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }

        public RecipeConfigModel Mending(RecipeConfig entity)
        {
            RecipeConfigModel model = _mapper.Map<RecipeConfigModel>(entity);
            RecipeType type = _recipeTypeOperate.GetRecipeTypeById(entity.RecipeTypeId);
            if (type != null)
            {
                model.RecipeType = type;
            }
            else
            {
                model.RecipeType = new RecipeType() {
                    RecipeTypeId = entity.RecipeTypeId,
                };
            }
            return model;
        }
        public List<RecipeConfig> GetAllRecipeConfigs()
        {
            try
            {
                return _recipeConfigOperate.GetAllRecipeConfigList();
            }
            catch (Exception ex)
            {
                return new List<RecipeConfig>();
            }
        }

        public RecipeConfig GetRecipeConfigById(string id)
        {
            try
            {
                return _recipeConfigOperate.GetRecipeConfigById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addRecipeConfig(RecipeConfig entity)
        {
            try
            {
                _recipeConfigOperate.AddRecipeConfig(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool updateRecipeConfig(RecipeConfig model)
        {
            try
            {
                RecipeConfig entity = _recipeConfigOperate.GetRecipeConfigById(model.ParameterId);
                _mapper.Map(model, entity);
                _recipeConfigOperate.UpdateRecipeConfig(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteRecipeConfig(string id)
        {
            try
            {
                RecipeConfig entity = _recipeConfigOperate.GetRecipeConfigById(id);
                if (entity != null)
                {
                    _recipeConfigOperate.DeleteRecipeConfig(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelRecipeConfig(List<string> ids)
        {
            try
            {
                List<RecipeConfig> entities = _recipeConfigOperate.GetRecipeConfigByIdList(ids);
                if (entities.Count > 0)
                {
                    _recipeConfigOperate.BatchDelRecipeConfig(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            } 
        }

        public List<RecipeConfig> GetRecipeConfigsByRecipeId(string id)
        {
            try
            {
               Recipe recipe = _recipeOperate.GetRecipeById(id);
                if (recipe != null)
                {
                    return _recipeConfigOperate.GetRecipeConfigsByTypeId(recipe.RecipeTypeId);
                }
            }catch (Exception ex)
            {
            }
            return new List<RecipeConfig>();
        }
    }
}
